Skip to content

Update welcome.js - #264

Open
RootSyntax16429 wants to merge 1 commit into
codebymitch:mainfrom
RootSyntax16429:patch-10
Open

RootSyntax16429 wants to merge 1 commit into
codebymitch:mainfrom
RootSyntax16429:patch-10

Conversation

@RootSyntax16429

Copy link
Copy Markdown

No description provided.

@RootSyntax16429

Copy link
Copy Markdown
Author

.

@RootSyntax16429

Copy link
Copy Markdown
Author

import { getColor } from '../../config/bot.js';
import {
SlashCommandBuilder,
PermissionFlagsBits,
ChannelType,
EmbedBuilder,
} from 'discord.js';
import { getWelcomeConfig, updateWelcomeConfig } from '../../utils/database.js';
import { formatWelcomeMessage, truncateForEmbedField } from '../../utils/welcome.js';
import { logger } from '../../utils/logger.js';
import { InteractionHelper } from '../../utils/interactionHelper.js';
import { ErrorTypes, replyUserError } from '../../utils/errorHandler.js';

function isValidHttpUrl(rawUrl) {
try {
const parsed = new URL(rawUrl);
return parsed.protocol === 'http:' || parsed.protocol === 'https:';
} catch {
return false;
}
}

export default {
name: 'welcome',
aliases: ['ترحيب', 'الترحيب', 'setup_welcome'],
category: 'admin',
data: new SlashCommandBuilder()
.setName('welcome')
.setDescription('Configure the welcome system')
.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild)
.addSubcommand((subcommand) =>
subcommand
.setName('setup')
.setDescription('Set up the welcome message')
.addChannelOption((option) =>
option
.setName('channel')
.setDescription('The channel to send welcome messages to')
.addChannelTypes(ChannelType.GuildText)
.setRequired(true)
)
.addStringOption((option) =>
option
.setName('message')
.setDescription('Welcome message. Variables: {user}, {username}, {server}, {memberCount}')
.setRequired(true)
)
.addStringOption((option) =>
option
.setName('image')
.setDescription('URL of the image to include in the welcome message')
.setRequired(false)
)
.addBooleanOption((option) =>
option
.setName('ping')
.setDescription('Whether to ping the user in the welcome message')
.setRequired(false)
)
),

async execute(interaction, args = [], clientArg = null) {
// دعم استدعاء الأمر كـ Slash Interaction فقط (لأن فيه subcommand و options)
if (!interaction?.isChatInputCommand?.() && !interaction?.options?.getSubcommand) {
if (interaction?.reply) {
await interaction
.reply('استخدم الأمر بصيغة السلاش /welcome setup لتحديد إعدادات الترحيب.')
.catch(() => {});
}
return;
}

try {
  const deferSuccess = await InteractionHelper.safeDefer(interaction);
  if (!deferSuccess) {
    logger.warn('Welcome interaction defer failed', {
      userId: interaction.user.id,
      guildId: interaction.guildId,
      commandName: 'welcome',
    });
    return;
  }
} catch (deferError) {
  logger.error('Welcome defer error', { error: deferError.message });
  return;
}

const { options, guild } = interaction;
const client = clientArg || interaction.client;

if (!guild) {
  return await replyUserError(interaction, {
    type: ErrorTypes.VALIDATION,
    message: 'This command can only be used inside a server.',
  });
}

if (!interaction.memberPermissions?.has(PermissionFlagsBits.ManageGuild)) {
  return await replyUserError(interaction, {
    type: ErrorTypes.PERMISSION,
    message: 'You need the **Manage Server** permission to use `/welcome`.',
  });
}

const subcommand = options.getSubcommand();

if (subcommand === 'setup') {
  const channel = options.getChannel('channel');
  const message = options.getString('message');
  const image = options.getString('image')?.trim() || null;
  const ping = options.getBoolean('ping') ?? false;

  if (!channel) {
    return await replyUserError(interaction, {
      type: ErrorTypes.VALIDATION,
      message: 'Please select a valid text channel.',
    });
  }

  const existingConfig = await getWelcomeConfig(client, guild.id);
  if (existingConfig?.channelId) {
    logger.info(
      `[Welcome] Setup blocked because config already exists in channel ${existingConfig.channelId} for guild ${guild.id}`
    );
    return await replyUserError(interaction, {
      type: ErrorTypes.UNKNOWN,
      message: `Welcome is already configured for <#${existingConfig.channelId}>. Use **/greet dashboard** to customize channel, message, ping, or image.`,
    });
  }

  if (!message || message.trim().length === 0) {
    logger.warn(`[Welcome] Empty message provided by ${interaction.user.tag} in ${guild.name}`);
    return await replyUserError(interaction, {
      type: ErrorTypes.VALIDATION,
      message: 'Welcome message cannot be empty.',
    });
  }

  if (image && !isValidHttpUrl(image)) {
    logger.warn(`[Welcome] Invalid image URL provided by ${interaction.user.tag}: ${image}`);
    return await replyUserError(interaction, {
      type: ErrorTypes.VALIDATION,
      message: 'Please provide a valid image URL (must start with http:// or https://).',
    });
  }

  try {
    await updateWelcomeConfig(client, guild.id, {
      enabled: true,
      channelId: channel.id,
      welcomeMessage: message.trim(),
      welcomeImage: image || undefined,
      welcomePing: ping,
    });

    logger.info(
      `[Welcome] Setup configured by ${interaction.user.tag} for guild ${guild.name} (${guild.id})`
    );

    const previewMessage = formatWelcomeMessage(message.trim(), {
      user: interaction.user,
      guild,
    });

    const embed = new EmbedBuilder()
      .setColor(getColor('success'))
      .setTitle('Welcome System Configured')
      .setDescription(`Welcome messages will now be sent to ${channel}`)
      .addFields(
        { name: 'Message Preview', value: truncateForEmbedField(previewMessage) },
        { name: 'Ping User', value: ping ? 'Yes' : 'No', inline: true },
        { name: 'Status', value: 'Enabled', inline: true }
      )
      .setFooter({ text: 'Tip: Use /greet dashboard to customize welcome settings' });

    if (image) {
      embed.setImage(image);
    }

    await InteractionHelper.safeEditReply(interaction, { embeds: [embed] });
  } catch (error) {
    logger.error(`[Welcome] Failed to setup welcome system for guild ${guild.id}:`, error);
    await replyUserError(interaction, {
      type: ErrorTypes.UNKNOWN,
      message: 'An error occurred while configuring the welcome system. Please try again.',
    });
  }
}

},
};

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant